package be.kuleuven.mume.rest;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.json.JSONArray;
import org.json.JSONObject;

import be.kuleuven.mume.JSONHelper;
import be.kuleuven.mume.beans.PersoonBean;
import be.kuleuven.mume.beans.VakBean;
import be.kuleuven.mume.beans.VraagBean;
import be.kuleuven.mume.beans.VraagBean.VraagBeanFactory;
import be.kuleuven.mume.query.PersoonQuery;
import be.kuleuven.mume.query.VakQuery;

@Path("/vraag")
public class Vraag {
	/**
	 * Rest interface for VraagBean objects
	 */
	private static final long serialVersionUID = 5522536807910452864L;
	private static final Logger log = Logger.getLogger(Vraag.class.getName());
	
	@GET
	@Path("/{vakid}")
	@Produces ("application/json")
	public String getVragen(@PathParam("vakid") String vakId, @QueryParam("rpp")@DefaultValue("10") long rpp, 
			@QueryParam("page")@DefaultValue("0") long page) {
		try{

			JSONArray array = new JSONArray();
			List<VraagBean> vragen = VraagBeanFactory.getVragenFromVak(vakId, page, rpp);

			for (VraagBean vraagBean : vragen) {
				array.put((new JSONObject(vraagBean)));
			}
			
			JSONObject result = new JSONObject();
			result.put("result", array);

			return result.toString();

		} catch (Exception e){
			log.log(Level.SEVERE,"Exception",e);
		} /*catch (UserNotLoggedInException e) {
			return e.toJsonString();
		}*/

		return JSONHelper.ERROR;

		//Check VraagBean.VraagBeanFactory.getVragenFromVak()
	}
	
	@POST
	@Path("/add")
	@Produces ("application/json")
	@Consumes ("application/json")
	//@Consumes ("plain/text")
	public String addVraag(String vraagJson){
		try{
			System.out.println("Test....."+vraagJson);
			JSONObject object = new JSONObject(vraagJson);
			VakBean vak = VakQuery.getVak(object.getString("vakId"));
			PersoonBean persoon = PersoonQuery.getCurrentPersoon();
			String onderwerp = object.getString("subject");
			String text = object.getString("text");
			VraagBean vraag = new VraagBean(persoon, vak, onderwerp, text);
			System.out.println("Test....."+object.getString("subject"));
			vraag.store();
			System.out.println("Test....."+object.getString("text"));
			return "{\"result\": \"succes\",\"vraagId\":\""+ vraag.getVakId() + "\"}";
			
		} catch (Exception e) {
			log.log(Level.SEVERE, "Exception", e);
		}
		return JSONHelper.ERROR;
	}
	
	@POST
	@Path("/update")
	@Produces ("application/json")
	@Consumes ("application/json")
	public String updateVraag(String vraagJson) {
		try {
			JSONObject object = new JSONObject(vraagJson);
			VraagBean vraag = VraagBeanFactory.getVraagById(object.getString("id"));
			if(object.has("subject"))
				vraag.setSubject(object.getString("subject"));
			if(object.has("text"))
				vraag.setText(object.getString("text"));
			
			vraag.store();
			
			return JSONHelper.SUCCES;
		} catch (Exception e) {
			log.log(Level.SEVERE, "Exception", e);
		}
		return JSONHelper.ERROR;
	}
	
	@GET
	@Path("/delete/{vraagid}")
	@Produces ("application/json")
	public String deleteVraag(@PathParam("vraagid")String vraagid) {
		return JSONHelper.NOTIMPLEMENTED;
	}
	
}
